package com.eazyimage.lab.controller;

import com.eazyimage.lab.entity.Response;
import com.eazyimage.lab.entity.parmas.DownParam;
import com.eazyimage.lab.service.DataService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Pzi
 * @create 2022-05-03 16:02
 */
@Slf4j
@RestController
@RequestMapping("/data")
public class DataController {

    @Autowired
    private DataService dataService;

    // 根据userName或者文件名查找文件
    @RequestMapping("/findFile")
        public Response findFile(@RequestBody String searchMsg){
        return dataService.findFile(searchMsg);
    }

    /**
     * 查询所有的文件数据
     * @return
     */
    @GetMapping("/allFileData")
    public Response findAllFileData(){
        return dataService.findAllFileData();
    }

    @RequestMapping("/delete/{id}")
    public Response deleteFileById(@PathVariable("id") Integer id){
        return dataService.deleteFileById(id);
    }

    //  传入文件路径，下载用户上传的zip文件
    @RequestMapping("/export")
    public void export(@RequestBody DownParam downParam, HttpServletResponse response){
//        log.warn("*********{}********",filePath);
//        int beginIndex = filePath.lastIndexOf(":");
//        String fileName = filePath.substring(beginIndex-2);

        dataService.export(downParam,response);
    }


    //  传入文件路径，下载用户上传的zip文件
    @RequestMapping("/exportData")
    public void export(@RequestParam("filePath") String filePath, HttpServletResponse response){

        System.out.println("a标签下载");
        dataService.exportData(filePath,response);
    }
}
